package com.maxima.attendance.actions;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;

import com.google.gson.Gson;
import com.maxima.attendance.daos.ProjectDAO;
import com.maxima.attendance.daos.UserDAO;
import com.maxima.attendance.pojos.User;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private ProjectDAO projectDAO = new ProjectDAO();
	private Integer id;
	private String jsonResponse = "";
	// for logging
	static Logger logger = Logger.getLogger(ProjectAction.class.getName());
	private UserDAO userDAO = new UserDAO();

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	@Action(value = "addNewUser", results = {
	@Result(name = "success", type = "json"), @Result(name = "error", type = "json")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String addNewUser() {
		User user = extractUser();
		if (userDAO.checkIfAlreadyPresent(user.getEmployeeId())) {
			jsonResponse = new Gson().toJson("error");
			logger.info("New Employee - " + user.getFirstName() + " Id - " + user.getId() + " - is not added Successfully");
			return ERROR;
		}
		userDAO.add(user);
		logger.info("New Employee - " + user.getFirstName() + " Id - " + user.getId() + " - added Successfully");
		jsonResponse = new Gson().toJson("success");
		return SUCCESS;
	}

	private User extractUser() {
		HttpServletRequest request = ServletActionContext.getRequest();
		User user = new User();
		user.setId(id);
		user.setFirstName(request.getParameter("firstname"));
		user.setLastName(request.getParameter("lastname"));
		String employeeId = request.getParameter("employeeId");
		user.setEmployeeId(employeeId != null ? Integer.parseInt(employeeId) : 0);
		user.setPassword(request.getParameter("password"));
		String projectId = request.getParameter("project");
		if (projectId != null) {
			user.setProject(projectDAO.get(Integer.parseInt(projectId)));
		}
		user.setActive("active".equals(request.getParameter("getstatus")));
		return user;
	}

	@Action(value = "initEditUserScreen", results = {
		@Result(name = "success", type = "json")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String selectUser() throws Exception {
		User user = userDAO.getUser(id);
		user.setId(id);
		try {
			jsonResponse = new Gson().toJson(user);
		}
		catch (Exception e) {
			logger.info(e.getMessage());
		}
		return SUCCESS;
	}

	@Action(value = "updateUser", results = {
	@Result(name = "success", type = "json"), @Result(name = "error", type = "json")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String updateUser() throws Exception {
		User user = extractUser();
		userDAO.update(user);
		logger.info("Employee - " + user.getFirstName() + " Id - " + user.getId() + " - updated Successfully");
		jsonResponse = new Gson().toJson(user);
		return SUCCESS;
	}

	public String getJsonResponse() {
		return jsonResponse;
	}

	public void setJsonResponse(String jsonResponse) {
		this.jsonResponse = jsonResponse;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
}